<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/16
 * Time: 17:56
 */

use library\core\db_conn;

/**
 * 银行账户操作类
 * Class account_class
 * @author zhangcn <wecontact@foxmail.com>
 */
class account_class extends db_conn
{
    /**
     * 转账账户余额
     * @var
     */
    public $balance;

    /**
     * 收款账户余额
     * @var
     */
    public $to_balance;

    /**
     * 创建账户
     *
     * @param $uid 账户用户
     * @return \Dibi\Result|int
     * @throws \Dibi\Exception
     */
    public function createAccount($uid)
    {
        $time = time();
        $create_time = date('Y-m-d H:i:s', $time);

        $data = array(
            'uid' => $uid,
            'name' => generate_serial(),
            'created_time' => $create_time,
        );

        $ret = $this->db->query('INSERT INTO bank_account %v', $data);

        return $ret;
    }

    /**
     * 冻结账户
     *
     * @param $account 账户号
     * @return \Dibi\Result|int
     * @throws \Dibi\Exception
     */
    public function freezeAccount($account)
    {
        $time = time();
        $create_time = date('Y-m-d H:i:s', $time);

        $data = array(
            'is_freeze' => 1,
            'updated_time' => $create_time,
        );

        $where = array(
            'name' => $account
        );

        $ret = $this->db->query("UPDATE bank_account SET %a WHERE %and", $data, $where);

        return $ret;
    }

    /**
     * 获取账户
     *
     * @param $account 账户号
     * @return \Dibi\Row|false
     * @throws \Dibi\Exception
     */
    public function getAccount($account)
    {
        $query = $this->db->query('SELECT * FROM bank_account WHERE binary name = ?', $account);

        $result = $query->fetch();

        return $result;
    }

    /**
     * 创建账户操作流水
     *
     * @param $account 账户号
     * @param array $data 流水数据
     * @return bool 返回布尔值
     * @throws \Dibi\Exception
     * @author zhangcn <wecontact@foxmail.com>
     */
    public function createBill($account, array $data)
    {
        $ret = $this->db->query('INSERT INTO bank_account_bills %v', $data);

        if ($ret === false)
        {
            logger("Bill insert fail.account:{$account}");
            return false;
        }

        return true;
    }

    /**
     * 账户取款余额更新
     *
     * @param $account 银行账户号
     * @param $balance 余额
     * @return bool 返回布尔值
     * @throws \Dibi\Exception
     */
    public function withholdUpdate($account, $balance)
    {
        $time = time();
        $create_time = date('Y-m-d H:i:s', $time);

        $data = array(
            'balance' => $balance,
            'updated_time' => $create_time,
        );

        $where = array(
            'name' => $account
        );

        $ret = $this->db->query("UPDATE bank_account SET %a WHERE %and", $data, $where);

        if ($ret === false)
        {
            logger("Withhold update fail.account:{$account}");
            return false;
        }

        return true;
    }

    /**
     * 账户存款余额更新
     *
     * @param $account 银行账户号
     * @param $balance 余额
     * @return bool 返回布尔值
     * @throws \Dibi\Exception
     */
    public function depositUpdate($account, $balance)
    {
        $time = time();
        $create_time = date('Y-m-d H:i:s', $time);

        $data = array(
            'balance' => $balance,
            'updated_time' => $create_time,
        );

        $where = array(
            'name' => $account
        );

        $ret = $this->db->query("UPDATE bank_account SET %a WHERE %and", $data, $where);

        if ($ret === false)
        {
            logger("Deposit update fail.account:{$account}");
            return false;
        }

        return true;
    }

    /**
     * 同一用户的不同账户之间转账
     *
     * @param $account 转账账户号
     * @param $to_account 收款账户号
     * @param $amount 转账金额
     * @return bool 返回布尔值
     * @throws \Dibi\Exception
     */
    public function transferAccount($account, $to_account, $amount)
    {
        // 扣款
        $balance = $this->balance - $amount;
        if ($this->withholdUpdate($account, $balance)) {
            $this->balance = $balance;

            // 存款
            $to_balance = $this->to_balance + $amount;
            if ($this->depositUpdate($to_account, $to_balance)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取当天转账金额总和
     *
     * @return \Dibi\Result|int
     * @throws \Dibi\Exception
     */
    public function getCurrentDayTransfer()
    {
        $begintime = date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
        $endtime = date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')+1,date('Y')));

        $query = $this->db->query('SELECT sum(amt) as total_amt FROM bank_account_bills WHERE bill_time > ? AND bill_time < ?', $begintime, $endtime);

        $result = $query->fetchSingle();

        return $result;
    }

    /**
     * 不同用户的账户之间转账
     *
     * @param $account 转账账户号
     * @param $to_account 收款账户号
     * @param $amount 转账金额
     * @return bool 返回布尔值
     * @throws \Dibi\Exception
     */
    public function transferDiffAccount($account, $to_account, $amount)
    {
        // 扣款
        $balance = $this->balance - $amount - TRANSFER_FEE;
        if ($this->withholdUpdate($account, $balance)) {
            $this->balance = $balance;

            // 存款
            $to_balance = $this->to_balance + $amount;
            if ($this->depositUpdate($to_account, $to_balance)) {
                return true;
            }
        }

        return false;
    }
}